package com.appheader.webfragmentviewpagertest.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.appheader.webfragmentviewpagertest.R;
import com.appheader.webfragmentviewpagertest.listener.OnRecyclerViewItemClickListener;
import com.appheader.webfragmentviewpagertest.viewholder.VerticalItemViewHolder;

import java.util.List;

/**
 * Created by zhep on 2016/9/28.
 * 垂直方向的recyclerview的adapter构建
 * 包括item监听事件
 */
public class VerticalRecyclerAdapter extends RecyclerView.Adapter<VerticalItemViewHolder> {
    private Context context;
    //    private List<CloudEntity> list = new ArrayList<>();
    private List<Integer> list;
    private OnRecyclerViewItemClickListener itemListener;

    public VerticalRecyclerAdapter(List<Integer> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public VerticalItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.img_item, parent, false);
        VerticalItemViewHolder viv = new VerticalItemViewHolder(itemView, itemListener);
        return viv;
    }
    @Override
    public void onBindViewHolder(VerticalItemViewHolder holder, final int position) {
        holder.imageView.setImageResource(list.get(position));
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"lksjdlflksdjslkdjksjdl"+position,Toast.LENGTH_SHORT).show();
                itemListener.onItemClick(v,position);//这个position是垂直方向的item位置
            }
        });
    }
    @Override
    public int getItemCount() {
        return list.size();
    }

//    @Override
//    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(context).inflate(R.layout.img_item, parent, false);
//
//        view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (itemListener!=null){
//                    itemListener.onItemClick(v, (String) v.getTag());
//                }
//            }
//        });
//        VerticalItemViewHolder holder = new VerticalItemViewHolder(view);
//        return holder;
//    }
//
//    @Override
//    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//
//        if (holder instanceof VerticalItemViewHolder) {
//            ((VerticalItemViewHolder) holder).imageView.setImageResource(list.get(position));
//            ((VerticalItemViewHolder) holder).itemView.setTag(position);


    //设置图片的点击事件跳转到对应webview展示
//            ((VerticalItemViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    Toast.makeText(context,"webview"+list.get(position),Toast.LENGTH_SHORT);
//                }
//            });
//        }
//    }

//    @Override
//    public int getItemCount() {
//        return list.size();
//    }

    //给item设置监听事件，正常要加回调把对应数据传出去
    //问题：item获取不到焦点，解决方法在布局中设置android:descendantFocusability="afterDescendants"
//    @Override
//    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//        Toast.makeText(context, "webview" + list.get(position), Toast.LENGTH_SHORT);
//        Log.e("TAG", "onItemClick: "+list.get(position));
//    }

    //    class VerticalItemViewHolder extends RecyclerView.ViewHolder {
//
//        ImageView imageView;
//
//        public VerticalItemViewHolder(View itemView) {
//            super(itemView);
//            imageView = (ImageView) itemView.findViewById(R.id.img);
//        }
//    }
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.itemListener = listener;
    }

//    public interface OnRecyclerViewItemClickListener{
//        void  onItemClick(View v,String tag);
//    }
}
